/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import Controller.MapBean;
import Model.PostInterface;
import Model.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.primefaces.context.RequestContext;

/**
 *
 * @author pedro
 */
public class PostUtil {
    
    public static String urlServer=ConfUtil.url;

    public static ArrayList<PostInterface> getCoordenates() throws JSONException {


        Gson g = new Gson();
        PostInterface post = new PostInterface();
        ArrayList<PostInterface> coordenates = new ArrayList();
        JSONArray values = new JSONArray();

        try {
            URL url = new URL(urlServer+"post");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "text/plain");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
        //    BufferedReader reader = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String output;

            Type collectionType = new TypeToken<List<PostInterface>>() {
            }.getType();
            while ((output = br.readLine()) != null) {

                coordenates = g.fromJson(output, collectionType);

            }

            conn.disconnect();




        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return coordenates;

    }
    public static void deletaPost(PostInterface post) {
        System.out.println("DeletaPost");
        Gson g = new Gson();
        String input = g.toJson(post);
        try {

            RequestContext context = RequestContext.getCurrentInstance();
            FacesMessage msg = null;
            boolean loggedIn = false;

            URL url = new URL(urlServer+"deletaPost");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");

            OutputStream os = conn.getOutputStream();
            os.write(input.getBytes());
            os.flush();

            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
            }
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Informação", "Post deletado Com Sucesso");
             MapBean mp= new MapBean();

            FacesContext.getCurrentInstance().addMessage(null, msg);
           

            conn.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
       
        
    }
}
